//询价单列表

interface IInquiryViewModel extends IBaseViewModel<IInquiryQueryTerms> {
    /**
     * ui-grid控制
     */
    gridOptions: uiGrid.IGridOptionsOf<InquiryListItemResponse>;
    /**
     * 时间日期格式
     */
    dateFormat: "{format:'YYYY.MM.DD HH:mm'}";
    /**
     * 列表高度
     */
    heightA:number;
    /**
     * 询价状态下拉
     */
    inquiryStatusDropDown: ValueListItemResponse[];
    /**
     * 发货单新增
     */
    orderAdd: () => void;
    /**
     * 询价订单添加-复制
     */
    addInquiry: () => void;
    /**
     * 询价单编辑
     */
    editInquiry: (id:string) => void;
    /**
     * 终结询价单
     */
    stopInquiry: (id: string, state: string, remarks: string) => void;
    /**
     * 删除询价单
     */
    delInquiry: (id: string) => void;
}

/**
 * 查询条件
 */
interface IInquiryQueryTerms {
    /**
     * 总询价单编号
     */
    inquiryId: string;
    /**
     * 子询价单编号
     */
    inquiryChildId: string;
    /**
     * 客户单位名称
     */
    clientName: string;
    /**
     * 询价状态
     */
    state: string;
    /**
     * 下单起始时间
     */
    beginTime: string;
    /**
     * 下单结束时间
     */
    endTime: string;
    /**
     * 发货地址
     */
    startAddress: string;
    /**
     * 送货地址
     */
    endAddress: string;
}

class InquiryController extends ListController<IInquiryViewModel, IInquiryQueryTerms> {
    gridApi: uiGrid.IGridApiOf<InquiryListItemResponse>;
    orderBy: number = 7;
    asc: boolean = false;
    selectedId: string;

    constructor($scope: IInquiryViewModel, private inquiryService: IInquiryService, private valueService: IValueService, private $ngBootbox: BootboxService, private $state: angular.ui.IStateService,private $location:ng.ILocationService) {
        super($scope);
        this.init();
    }

    init(): void {
        super.init();
        
        this.$scope.orderAdd = this.orderAdd;
        this.$scope.addInquiry = this.addInquiry;
        this.$scope.editInquiry = this.editInquiry;
        this.$scope.stopInquiry = this.stopInquiry;
        this.$scope.delInquiry = this.delInquiry;
        this.$scope.$on("WildDog:Inquiry1", this.onWilddogOrderEvent);
        this.$scope.queryTerms = {
            inquiryId: "",
            inquiryChildId: "",
            clientName: "",
            state: "",
            beginTime: "",
            endTime: "",
            startAddress: "",
            endAddress: "",
        };

        this.$scope.gridOptions = {
            enableSelectAll: false,
            multiSelect:false,
            enableFullRowSelection: true,
            paginationPageSizes: [10, 20, 30],
            useExternalPagination: true,
            useExternalSorting: true,
            enableColumnResizing: true,
            columnDefs: [
                { displayName: "总询价编号", field: 'inquiryId', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, width: '9%' },
                { displayName: "子询价编号", field: 'inquiryChildId', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, width: '10%' },
                { displayName: "客户单位", field: 'clientName', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, width: '12%' },
                { displayName: '发货地址', field: 'startAddress', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, enableSorting: false, width: '12%' },
                { displayName: '送货地址', field: 'endAddress', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, enableSorting: false, width: '12%' },
                { displayName: '货物名称', field: 'goodsName', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, },
                { displayName: '货物数量', field: 'goodsQuantity', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, },
                { displayName: '所需车长', field: 'carLength', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, width: '7%' },
                { displayName: '下单时间', field: 'createTime', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, width: '9%' },
                { displayName: '紧急程度', field: 'urgency', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, },
                { displayName: '询价状态', field: 'state', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, },
                {
                    name: "操作",
                    cellFilter: 'checkmark',
                    cellTemplate: '<div ng-if="row.entity.state == \'退回下单\'">'
                    + '<a class="fa fa-minus-circle primary text-primary m-l-xs" ng-click="grid.appScope.stopInquiry(row.entity.id)" title="终结订单" ></a>'
                    + '<a class="fa fa-edit m-l-xs  primary text-info" title="编辑重下" ng-click="grid.appScope.editInquiry(row.entity.id)" ></a>'
                    + '</div>'
                    + '<div ng-if="row.entity.state == \'应付报价\'">'
                    + '<a class="fa fa-minus-circle primary text-primary m-l-xs" ng-click="grid.appScope.stopInquiry(row.entity.id)" title="终结订单"></a>'
                    + '<a class="primary fa fa-trash-o text-danger m-l-xs" title="删除" ng-click="grid.appScope.delInquiry(row.entity.id)"></a>'
                    + '</div>'
                    + '<div ng-if="row.entity.state == \'询价终结\' ">'
                    + '<sapn class=" primary text-primary m-l-xs"></span>'
                    + '</div>',
                    enableColumnMenu: false, enableSorting: false, width: '4%'
                }
            ],
            onRegisterApi: (gridApi) => {
                this.gridApi = gridApi;
                gridApi.pagination.on.paginationChanged(this.$scope, this.paginationChanged);
                gridApi.core.notifyDataChange("options");
                gridApi.core.on.sortChanged(this.$scope, this.sortChange);
                gridApi.selection.on.rowSelectionChanged(this.$scope, this.selectChange);
            }
        };
        this.query();
        this.loadInquiryStatusDropDown();
        $('#opendataTime').click(() => {
            $('#dataTime').datetimepicker('show');
        });

        $('#opendataTimeEnd').click(()=> {
            $('#dataTimeEnd').datetimepicker('show');
        });
    }

    localHistory():void{
        super.localHistory(this.$state);
    }
    getHistory():void{
        super.getHistory(this.$state,this.$location)
    }

    /**
     * 加载数据
     */
    loadData(queryTerms: IInquiryQueryTerms, skip: number, count: number): void {
        this.inquiryService.getList(this.orderBy.toString(), this.asc, queryTerms.inquiryId, queryTerms.inquiryChildId,
            queryTerms.clientName, "", "",queryTerms.startAddress, queryTerms.endAddress, queryTerms.state, queryTerms.beginTime, queryTerms.endTime, skip, count).then(result => {
                this.$scope.gridOptions.totalItems = result.data.total;
                this.$scope.gridOptions.data = result.data.data;
                /**
                 * 设置列表高度
                 */
                this.$scope.heightA = (this.$scope.gridOptions.data.length*39) + 80;

                if (skip == 0) {
                    this.$scope.gridOptions.paginationCurrentPage = 1;
                }
                this.$scope.querying = false;
            }, error => {
                this.$scope.querying = false;
            });
    }

    /**
     * 监听野狗事件
     */
    onWilddogOrderEvent = (event: ng.IAngularEvent, data: any) => {
        this.query();
    }

    /**
     * 询价状态下拉
     */
    loadInquiryStatusDropDown(): void {
        this.$scope.inquiryStatusDropDown = this.valueService.getInquiryStatusList().data;
    }

    /**
     * 排序事件触发
     */
    sortChange = (grid: uiGrid.IGridInstanceOf<InquiryListItemResponse>, sortColumns: Array<uiGrid.IGridColumnOf<InquiryListItemResponse>>) => {
        if (sortColumns.length == 0) {
            this.query();
        } else {
            let sortItem = sortColumns[0];
            switch (sortItem.name) {
                case "inquiryId": {
                    this.orderBy = 1;
                } break;
                case "inquiryChildId": {
                    this.orderBy = 2;
                } break;
                case "clientName": {
                    this.orderBy = 3;
                } break;
                case "goodsName": {
                    this.orderBy = 4;
                } break;
                case "goodsQuantity": {
                    this.orderBy = 5;
                } break;
                case "carLength": {
                    this.orderBy = 6;
                } break;
                case "createTime": {
                    this.orderBy = 7;
                } break;
                case "urgency": {
                    this.orderBy = 8;
                } break;
                case "state": {
                    this.orderBy = 9;
                } break;
            }
            this.asc = sortItem.sort.direction == "asc";
            this.query();
        }
    }

    /**
     * 单选事件
     */
    selectChange = (row: uiGrid.IGridRowOf<InquiryListItemResponse>) => {
        if(row.isSelected){
            this.selectedId = row.entity.id
        }
        else{
            this.selectedId = "";
        }
    }

    /**
     * 发货单新增
     */
    orderAdd = () => {
        if (!this.selectedId) {
            this.$ngBootbox.alert("请选择一项");
            return;
        };
        this.$state.go("app.order.orderAdd", { id: this.selectedId, name: "inquiry" });
    };

    /**
     * 询价单新增
     */
    addInquiry = () => {
        if (this.selectedId == undefined || this.selectedId == "") {
            this.$state.go("app.inquiry.inquiryAdd", { name: 'Add' });
        } else {
            this.$state.go("app.inquiry.inquiryAdd", { id: this.selectedId, name: 'copy' })
        }
    }

    /**
     * 询价单编辑重下
     */
    editInquiry = (id:string) => {
        this.$state.go("app.inquiry.inquiryAdd", { id: id, name: 'edit' })
    }

    /**
     * 终结询价单
     */
    stopInquiry = (id: string) => {
        this.$ngBootbox.confirm("是否终止该订单？").then(e => {
            this.inquiryService.state(id,"12", "").then(result => {
                this.reload();
            });
        });
    }

    /**
     * 删除询价单
     */
    delInquiry = (id: string) => {
        this.$ngBootbox.confirm("是否删除该订单？").then(e => {
            this.inquiryService.delete(id).then(result => {
                this.reload();
            });
        });
    }
}

angular.module('tms').controller("inquiryController", ["$scope","inquiryService","valueService","$ngBootbox","$state",'$location', InquiryController]);